<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
/*public */function passportmodel_members($page,$pageline) {
    $dbmodel = xt_load('model','databasemodel');

    $intkeys = array('uid'=>'uid');
	$strkeys = array('username'=>'username');
	$randkeys = array('dateline'=>array('sstrtotime','dateline'), 'updatetime'=>array('sstrtotime','updatetime'));
	$likekeys = array();
	$results = $dbmodel('getwheres',$intkeys, $strkeys, $randkeys, $likekeys);
	$wherearr = $results['wherearr'];
	$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);
    $mpurl = count($results['urls'])>0?'&'.implode('&', $results['urls']):'';

	$orders = $dbmodel('getorders',array('dateline'=>'dateline', 'updatetime'=>'updatetime'), '');
	
	$ordersql = $orders['sql'];
	if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
	
	$r = $dbmodel('multipage',"SELECT * FROM ".xt_tname('members')." WHERE $wheresql", $ordersql, $page, $pageline);
	$r['pageurl'] = $mpurl;
    return  $r;
}
/*public */function passportmodel_mlogin($username,$password) {
    $db = xt_load('db');
    $query = $db->query("SELECT m.uid,m.salt,m.password,a.roles FROM ".xt_tname('members').' m,'.xt_tname('managers').
        " a WHERE a.username= '$username' and m.uid=a.uid");
    if($db->num_rows($query)>0){
        $m = $db->fetch_array($query);
        return $m['password'] == md5(md5($password).$m['salt'])? $m :FALSE;
    }
    return FALSE;
}
/*public */function passportmodel_managers() {
    $db = xt_load('db');
    $roles = xt_load('cache','roles');
    $query = $db->query("SELECT * FROM ".xt_tname('managers'));
    $result = array();
    while ($row = $db->fetch_array($query)) {
        $rolenames = array();
        foreach($roles as $r) {
            if((intval($r[0]) & $row['roles'])>0 ) {$rolenames[] =$r[1];}
        }
        $row['rolelist'] =  implode($rolenames,' | '); 
        $result[] = $row;
	}
    return $result;
}
/*public */function passportmodel_findManagerByUserId($uid) {
    $db = xt_load('db');
    return $db->fetch_first("SELECT * FROM ".xt_tname('managers')." WHERE uid= '$uid'");
}
/*public */function passportmodel_findMemberByUserId($uid) {
    $db = xt_load('db');
    return $db->fetch_first("SELECT * FROM ".xt_tname('members')." WHERE uid= '$uid'");
}
/*public */function passportmodel_findMemberByUserName($username) {
    $db = xt_load('db');
    return $db->fetch_first("SELECT * FROM ".xt_tname('members')." WHERE username= '$username'");
}
/*public */function passportmodel_repalceManager($uid, $username,$roles) {
    $db = xt_load('db');
    $db->inserttable('managers',array('uid'=>$uid, 'username'=>$username,'roles'=>$roles),0,TRUE);
}
/*public */function passportmodel_updateManager($uid, $roles) {
    $db = xt_load('db');
    $db->updatetable('managers',array('roles'=>$roles),array('uid'=>$uid));
}
/*public */function passportmodel_deleteManagers($uids) {
    $db = xt_load('db');
    $uids = addslashes($uids);
    $db->query('DELETE FROM '.xt_tname('managers')." WHERE uid in($uids)");
}
?>